function getMaxMin(slice, high, low) {
    let max = slice[0][high];
    let min = slice[0][low];
    slice.forEach(data => {
        max = Math.max(max, data[high]);
        min = Math.min(min, data[low]);
    });
    return [max, min];
}

// 计算全局中轴
function getGlobalMid(dataList) {
    let total = 0;
    dataList.forEach(data => {
        total += (data.high + data.low) / 2;
    });
    return total / dataList.length;
}

// 以全局中轴翻转单条K线
function flipCandleGlobal(kLineData, globalMid) {
    return {
        open: 2 * globalMid - kLineData.close,
        close: 2 * globalMid - kLineData.open,
        high: 2 * globalMid - kLineData.low,
        low: 2 * globalMid - kLineData.high,
        volume: kLineData.volume
    };
}

function calcKDJ(dataList, dir) {
    const kdjr = [];
    for (let i = 0; i < dataList.length; i++) {
        const kdj = {};
        const close = dataList[i].close;
        if (i >= 8) {
            const [hn, ln] = getMaxMin(dataList.slice(i - 8, i + 1), 'high', 'low');



            const rsv = (close - ln) / (hn - ln === 0 ? 1 : hn - ln) * 100;

            kdj.k = (100 + rsv) / 3;
            kdj.d = (100 + kdj.k) / 3;

            if(i==100 && dir == 1){
                console.log("currKDHNLN", hn, ln, rsv, kdj.k, kdj.d);
            }
        }
        kdjr.push(kdj);
    }
    return kdjr;
}

function calcAngle(curr, prev, currKD, prevKD) {
    const highSlope = curr.high / prev.low;
    const kSlope = (currKD.k / prevKD.k) / (currKD.d / prevKD.d);
    return Math.atan(highSlope * kSlope) * 180 / Math.PI;
}

const stoch = {
    name: 'SLOPE_SIGNAL',
    shortName: 'SLOPE_SIGNAL',
    calcParams: [20],
    figures: [
        { key: 'highSlope', title: 'High Slope', type: 'line' },
        { key: 'sellSignal', title: 'Sell Signal', type: 'line' },
        { key: 'signalDirection', title: 'Signal Direction', type: 'line' }
    ],
    calc: (dataList, indicator) => {
        const globalMid = getGlobalMid(dataList);  // 👈 用全局中轴做翻转
        const dataListDown = dataList.map(k => flipCandleGlobal(k, globalMid));
        const kdjUp = calcKDJ(dataList, 1);
        const kdjDown = calcKDJ(dataListDown, 0);

        const results = [];

        for (let i = 0; i < dataList.length; i++) {
            const result = {
                highSlope: -1,
                sellSignal: -1,
                signalDirection: 0
            };

            if (i < 8) {
                results.push(result);
                continue;
            }

            const curr = dataList[i];
            const prev = dataList[i - 1];
            const currKD = kdjUp[i];
            if(i == 9) {
                console.log("currKD", i, currKD);
            }
            const prevKD = kdjUp[i - 1];

            const angleUp = calcAngle(curr, prev, currKD, prevKD);
            if (angleUp >= 48) {
                result.highSlope = angleUp;
            }

            const currDown = dataListDown[i];
            const prevDown = dataListDown[i - 1];
            const currKDDown = kdjDown[i];
            const prevKDDown = kdjDown[i - 1];

            const angleDown = calcAngle(currDown, prevDown, currKDDown, prevKDDown);
            if (angleDown >= 48) {
                result.sellSignal = -angleDown;
            }

            result.signalDirection = (currKD.k - currKD.d) / 100;
            results.push(result);
        }

        return results;
    }
};

export default stoch;
